﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Model
{
    public class Word
    {
        private Verse verse = null;
        public Verse Verse
        {
            get { return verse; }
        }

        private int number = 0;
        public int Number
        {
            set { number = value; }
            get
            {
                if (number == 0)
                {
                    if (verse.Book != null)
                    {
                        foreach (Verse v in verse.Book.Verses)
                        {
                            if (v.Number == verse.Number)
                            {
                                number += number_in_verse;
                                break;
                            }
                            number += v.Words.Count;
                        }
                    }
                }
                return number;
            }
        }

        private int number_in_chapter = 0;
        public int NumberInChapter
        {
            set { number_in_chapter = value; }
            get
            {
                if (number_in_chapter == 0)
                {
                    if (verse != null)
                    {
                        if (verse.Chapter != null)
                        {
                            foreach (Verse v in verse.Chapter.Verses)
                            {
                                if (v.Number == verse.Number)
                                {
                                    number_in_chapter += number_in_verse;
                                    break;
                                }
                                number_in_chapter += v.Words.Count;
                            }
                        }
                    }
                }
                return number_in_chapter;
            }
        }

        private int number_in_verse;
        public int NumberInVerse
        {
            set { number_in_verse = value; }
            get { return number_in_verse; }
        }

        public string Address
        {
            get
            {
                if (verse != null)
                {
                    if (verse.Chapter != null)
                    {
                        return (this.verse.Chapter.Number.ToString() + ":" + verse.NumberInChapter.ToString() + ":" + number_in_verse.ToString());
                    }
                }
                return "XXX:XXX:XXX";
            }
        }

        private string transliteration = null;
        public string Transliteration
        {
            get
            {
                if (transliteration == null)
                {
                    if (this.Verse.Translations.ContainsKey("en.transliteration"))
                    {
                        string verse_transliteration = this.Verse.Translations["en.transliteration"];
                        string[] parts = verse_transliteration.Split(' ');
                        transliteration = parts[this.NumberInVerse - 1];
                    }
                }
                return transliteration;
            }
        }

        private string meaning;
        public string Meaning
        {
            set { meaning = value; }
            get { return meaning; }
        }

        private List<string> roots = null;
        public List<string> Roots
        {
            set { roots = value; }
            get { return roots; }
        }

        private int occurrence = 0;
        public int Occurrence
        {
            get
            {
                if (occurrence == 0)
                {
                    bool stop = false;
                    foreach (Verse verse in this.Verse.Book.Verses)
                    {
                        if (!stop)
                        {
                            foreach (Word word in verse.Words)
                            {
                                if (word.Text == this.text)
                                {
                                    occurrence++;
                                }

                                if (word == this)
                                {
                                    stop = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                return occurrence;
            }
        }

        private int occurrences = 0;
        public int Occurrences
        {
            get
            {
                if (occurrences == 0)
                {
                    foreach (Verse verse in this.Verse.Book.Verses)
                    {
                        foreach (Word word in verse.Words)
                        {
                            if (word.Text == this.Text)
                            {
                                occurrences++;
                            }
                        }
                    }
                }
                return occurrences;
            }
        }

        private string text;
        public string Text
        {
            get { return text; }
        }

        private int position;
        public int Position
        {
            get { return position; }
        }

        public Word(Verse verse, int number_in_verse, int position_in_verse, string text)
        {
            this.verse = verse;
            this.number_in_verse = number_in_verse;
            this.position = position_in_verse;
            this.text = text;
        }
    }
}
